package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.Supplier;
import com.ywgt.system.service.SupplierService;
import com.ywgt.system.vo.SupplierVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "供应商模块",tags ="供应商模块" )
@RequestMapping("/business/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value = "查询所有供应商",notes ="查询供应商" )
    @GetMapping("/findAll")
    public Result findAll(){
        List<Supplier> suppliers = supplierService.list();
        return Result.ok().data("suppliers",suppliers);
    }
    @ApiOperation(value = "根据条件查询",notes ="根据条件查询供应商" )
    @GetMapping("/findSupplierList")
    public Result findSupplierList(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Long pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "8",required = false)Long pageSize,
                                   Supplier supplier){
        Page<Supplier> page=new Page<>(pageNum,pageSize);
        Page<Supplier> supplierPage = supplierService.page(page,getWrapper(supplier));
        List<Supplier> records = page.getRecords();
        long total = page.getTotal();
        return Result.ok().data("rows",records).data("total",total);
    }
    @ApiOperation(value = "查询供应商信息",notes="根据id查询供应商信息" )
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable("id")Long id){
        Supplier supplier = supplierService.getById(id);
        return Result.ok().data("row",supplier);
    }
    @ApiOperation(value = "更新信息",notes ="更新供应商信息" )
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id")Long id,@RequestBody Supplier supplier){
        LambdaUpdateWrapper<Supplier> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Supplier::getId,id);
        if (supplierService.update(supplier,updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除信息",notes ="删除供应商信息" )
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        if (supplierService.removeById(id)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "添加信息",notes ="添加供应商信息" )
    @PostMapping("/add")
    public Result add(@RequestBody SupplierVO supplierVO){
        Supplier supplier=new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        if (supplierService.save(supplier)) {
            return Result.ok();
        }
        return Result.error();
    }


    public LambdaQueryWrapper<Supplier> getWrapper(Supplier supplier){
        LambdaQueryWrapper<Supplier> queryWrapper=new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(supplier.getAddress())){
            queryWrapper.like(Supplier::getAddress,supplier.getAddress());
        }
        if (!StringUtils.isEmpty(supplier.getContact())){
            queryWrapper.like(Supplier::getContact,supplier.getContact());
        }
        if (!StringUtils.isEmpty(supplier.getName())){
            queryWrapper.like(Supplier::getName,supplier.getName());
        }
        return queryWrapper;
    }
}

